package com.shoping.web.controller;

import com.shoping.common.response.ResponseData;
import com.shoping.web.entity.Student;
import com.shoping.web.fegin.OrderControllerClient;
import com.shoping.web.mapper.StudentMapper;
import com.shoping.web.po.request.OrderPo;
import com.shoping.web.po.response.OrderDto;
import com.shoping.web.service.TeacherService;
import com.shopping.alarm.ding.DingTalkTextMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api")
public class UserPortalController {

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    DingTalkTextMessage dingTalkTextMessage;

    @Autowired
    OrderControllerClient orderControllerClient;

    @Autowired
    TeacherService teacherService;

    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/student/{id}")
    public ResponseData<Student> getStudent(@PathVariable("id") Long id) {
        log.info("teacherService.getMyName() == " + teacherService.toString());
        Student student = studentMapper.selectByPrimaryKey(id);
        dingTalkTextMessage.sendText("查询数据库=" + student);
        return ResponseData.build(student);
    }

    @GetMapping("/order/{id}")
    public ResponseData<OrderDto> getOrder(@PathVariable("id") Integer id) {
        int size = discoveryClient.getInstances("shopping-order3-service").size();
        log.info("数量：{}", size);
        return orderControllerClient.getOrder(id);
    }

    @PostMapping("/order")
    public ResponseData<OrderDto> createOrder(@RequestBody @Validated OrderPo orderPo) {
        return orderControllerClient.saveOrder(orderPo);
    }
}
